/*
 * 		Author : Xerxes
 * 		Program: 
 * 		Created On : 27.09.2011 15:30:16 BDT
 *      "I have not failed, I have just found 10000 ways that won't work.
 * 
*/


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define fo(i,st,ed) for(int i = st ; i < ed ; ++i )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define tp pair< int, pair<int,int> >
#define mmk( x, y , z ) mk( x, mk( y, z ) )

inline void wait( double seconds ){
    double endtime = clock()+( seconds* CLOCKS_PER_SEC );
    while( clock() < endtime ){
        ;
    }
}
string feed="";
string line;
vector< vector< int > > go;
int time_need[ 30 ];
int incomming_edges[30];

inline vector< string > split(){
	stringstream ss(line);
	vector< string > parts;
	for(string temp=""; getline(ss,temp,' ');  ){
		if( LN(temp) < 1 ) continue;
		parts.pb( temp );
	}
return parts;
}
inline int toI(string ss){
	int d;
	sscanf(ss.c_str(),"%d",&d);
return d;
}
inline void Read(){	
	go.assign(30,vector<int>() );
	SET(time_need,-1);
	SET(incomming_edges,-1);
	while( getline(cin,line) ){
		if( LN(line) < 1 ){
			break;
		}
		vector< string > temp = split();
		int sz = SZ(temp);
		if( sz==2 ){
			int source = temp[0][0]-'A';
			int cost   = toI(temp[1]);
			time_need[ source ] = cost;
		}
		else{
			int source = temp[0][0]-'A';
			int cost   = toI(temp[1]);
			string cango = temp[2];
			time_need[ source ] = cost;
			incomming_edges[ source ] = 1;
			for(int i=0,j=SZ(cango); i<j;++i){
				go[source].pb( cango[i]-'A' );
			}
		}
	}
}
int done[30];
inline int recur(int nw_node){
	if( done[nw_node]==0 ){
		int var = time_need[nw_node];
		VI cango = go[nw_node];
		int max_value = 0;
		for(int i=0,j=SZ(cango); i<j; ++i ){
			max_value = max( max_value, recur( cango[i] ) );
		}
		done[ nw_node ] = max_value+var;
	}
	
return done[nw_node];
}
inline void Proc(){
	SET(done,0);
	
	for(int i=0;i<26;++i){
		if( time_need[i]==-1 )continue;
		if( done[i]!=0 )continue;
		
		if( incomming_edges[ i ]==-1 ){
			done[i] = time_need[i];
		}
		else{
			recur(i);
		}
	}
	int retval = 0;
	int prothom= 1;
	for(int i=0;i<26;++i){
		if( time_need[i]==-1 )continue;
		if( prothom ){
			prothom = 0;
			retval = done[i];
		}
		else{
			retval = max( retval, done[i] );
		}
	}
	
	printf("%s%d\n",feed.c_str(),retval);
	feed="\n";
}
int main(){
    #ifndef ONLINE_JUDGE
        freopen("in","rt",stdin);
        freopen("out","wt",stdout);
    #endif
    int cases=1;
    for( scanf("%d\n",&cases) ; cases ; --cases ){
        Read();
        Proc();
    }

return 0;
}


